﻿Imports System
Imports System.Xml
Imports System.Xml.Serialization
Imports System.IO
Imports Microsoft.VisualBasic
''' <summary>
''' 将对象或哈希表从文件读取或写入的方法集合
''' </summary>
''' <remarks></remarks>
Public Module IOSystemBase
    ''' <summary>
    ''' 从二进制文件中读取哈希表
    ''' </summary>
    ''' <param name="StrFileName">文件名</param>
    ''' <returns>哈希表</returns>
    ''' <remarks></remarks>
    Function ReadHashTableFromBinaryFile(ByVal StrFileName As String) As Hashtable
        Dim ReturnHashTable As Hashtable

        Dim formatter = New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
        Dim stream = New IO.FileStream(StrFileName, IO.FileMode.Open)
        Try
            ReturnHashTable = CType(formatter.Deserialize(stream), Hashtable)
        Catch
            ReturnHashTable = Nothing
        Finally
            stream.Close()
        End Try
        Return ReturnHashTable
    End Function
    ''' <summary>
    ''' 将哈希表写入二进制文件
    ''' </summary>
    ''' <param name="strFileName">文件名</param>
    ''' <param name="obj">哈希表</param>
    ''' <remarks></remarks>
    Sub WriteHashTableToBinaryFile(ByVal strFileName As String, ByVal obj As Hashtable)
        Dim formatter = New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
        Dim stream = New IO.FileStream(strFileName, IO.FileMode.OpenOrCreate)
        formatter.Serialize(stream, obj)
        stream.Close()
    End Sub
    ''' <summary>
    ''' 从XML文件中读取哈希表
    ''' </summary>
    ''' <param name="StrFileName">文件名</param>
    ''' <returns>哈希表</returns>
    ''' <remarks></remarks>
    Function ReadHashTableXMLFile(ByVal StrFileName As String) As Hashtable
        Dim ReturnHashTable As Hashtable
        Dim formatter = New System.Xml.Serialization.XmlSerializer(GetType(Hashtable))
        Dim stream = New IO.FileStream(StrFileName, IO.FileMode.Open)
        Try
            ReturnHashTable = CType(formatter.Deserialize(stream), Hashtable)
        Catch
            ReturnHashTable = Nothing
        Finally
            stream.Close()
        End Try
        Return ReturnHashTable
    End Function
    ''' <summary>
    ''' 将哈希表写入XML文件
    ''' </summary>
    ''' <param name="strFileName">文件名</param>
    ''' <param name="obj">哈希表</param>
    ''' <remarks></remarks>
    Sub WriteHashTableToXMLFile(ByVal strFileName As String, ByVal obj As Hashtable)
        Dim formatter = New System.Xml.Serialization.XmlSerializer(GetType(Hashtable))
        Dim stream = New IO.FileStream(strFileName, IO.FileMode.OpenOrCreate)
        formatter.Serialize(stream, obj)
        stream.Close()
    End Sub
    ''' <summary>
    ''' 将可XML序列化对象写入XML文件
    ''' </summary>
    ''' <param name="obj">对象</param>
    ''' <param name="mType">类型</param>
    ''' <param name="FileName">文件名</param>
    ''' <remarks>如果对象是不可序列化的类型，将返回错误</remarks>
    Sub WriteObjectToXMLFile(ByVal obj As Object, ByVal mType As Type, ByVal FileName As String)
        ' Use the constructor that takes a type and XmlRootAttribute.
        Dim serializer As New XmlSerializer(mType)
        Dim writer As New StreamWriter(FileName)
        serializer.Serialize(writer, obj)
        writer.Close()
    End Sub
    ''' <summary>
    ''' 从XML文件读取对象
    ''' </summary>
    ''' <param name="mType">类型</param>
    ''' <param name="FileName">文件名</param>
    ''' <returns>对象</returns>
    ''' <remarks>如果对象是不可序列化的类型，将返回错误</remarks>
    Function ReadObjectFromXMLFile(ByVal mType As Type, ByVal FileName As String) As Object
        Dim serializer As New XmlSerializer(mType)
        Dim Reader As New StreamReader(FileName)
        Dim Rtn As Object
        Rtn = serializer.Deserialize(Reader)
        Reader.Close()
        Return Rtn
    End Function
    ''' <summary>
    ''' 如果没有指定目录，则创建
    ''' </summary>
    ''' <param name="strDirectoryPath">指定目录</param>
    ''' <remarks></remarks>
    Sub CreateDirectoryWhenNotExists(ByVal strDirectoryPath As String)
        If IO.Directory.Exists(strDirectoryPath) = False Then
            IO.Directory.CreateDirectory(strDirectoryPath)
        End If
    End Sub
End Module
''' <summary>
''' 文件信息一览类(XML序列化用)
''' </summary>
''' <remarks></remarks>
<Serializable()> _
Public Class clsFileInfoList
    Public Property FileInfo() As List(Of CommonMethod.FileInfoRec)
    ''' <summary>
    ''' 通过文件名获得文件信息
    ''' </summary>
    ''' <param name="Filename"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function GetFileInfoByName(ByVal Filename As String) As CommonMethod.FileInfoRec
        If Not mFileInfoDic.ContainsKey(mIdentificationNode.ProgramID & "|" & Filename) Then
            Return Nothing
        Else
            Return mFileInfoDic.Item(mIdentificationNode.ProgramID & "|" & Filename)
        End If
    End Function
End Class
''' <summary>
''' COPY句信息一览类(XML序列化用)
''' </summary>
''' <remarks></remarks>
<Serializable()> _
Public Class clsCopyBookList
    Public Property CopyBookInfo() As List(Of CommonMethod.CopyBookInfoRec)
    ''' <summary>
    ''' 获取COPY文件信息
    ''' </summary>
    ''' <param name="CopyBookName"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function GetCopyBookInfoByName(ByVal CopyBookName As String) As CopyBookInfoRec
        '出现在PROC以后的COPY句，都会被放到INIT_DATA里面
        '如果这个COPY句确定是DATA的话，恢复到DATA里面去
        If Not mCopyBookInfoDic.ContainsKey(CopyBookName) Then
            Return Nothing
        Else
            Return mCopyBookInfoDic.Item(CopyBookName)
        End If
    End Function
End Class